import {
    addHistoryAction,
    getSearchHelper,
    getKeywordsAndHistory,
} from "@/api/search/search.js";
export default {
    namespaced: true,
    state() {
        return {
            historyData: [],
            hotKeywordList: [],
            searchVal: '',
            helperList: [],
            showComponents: "history",
            sort:'asc'
        }
    },
    mutations: {
        getHistoryAndHot(state, data) {
            state.historyData = data.historyData;
            state.hotKeywordList = data.hotKeywordList;
        },
        getInput(state, data) {
            state.searchVal = data;
        },
        gethelperList(state, data) {
            state.helperList = data.keywords;
        },
        changeShow(state, str) {
            state.showComponents = str;
        },
        changSort(state,str){
            state.sort = str;
        }
    },
    actions: {
        asyncGetHistoryAndHot({
            commit
        }) {
            let openId = localStorage.getItem("openId");
            getKeywordsAndHistory({
                openId: openId
            }).then((res) =>
                commit("getHistoryAndHot", res)
            );
        },
        asyncGethelperList({
            commit,
            state
        }) {
            let openId = localStorage.getItem("openId");
            if(state.searchVal){
                addHistoryAction({openId:openId,keyword:state.searchVal}).then(res=>console.log(res))
            }
            getSearchHelper({
                keyword: state.searchVal,
                order: state.sort,
            }).then((res) => commit("gethelperList", res));
        },
    },
    getters: {}
}